﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class ClassBLL
    {
        public List<Class> GetAll()
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Classes.OrderBy(p => p.Id).ToList();
            }
        }

        public List<Class> GetAll(bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Classes.Where(p => p.isEnable == isEnable).OrderBy(p => p.Id).ToList();
            }
        }

        //Get By Parent

        public List<Category> GetByParent(int idParent)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Categories.Where(p => p.Parent == idParent).OrderBy(p => p.Sort).ToList();
            }
        }

        public List<Class> GetByParentClass(int idParent)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Classes.Where(p => p.CateClassId == idParent).OrderBy(p => p.Id).ToList();
            }
        }

        public List<Class> GetByParentClass(int idParent, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Classes.Where(p => p.CateClassId == idParent&& p.isEnable == isEnable).OrderBy(p => p.Id).ToList();
            }
        }

        public Category GetByParentClassId(int idParent, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Categories.Where(p => p.Id == idParent && p.isEnable == isEnable).FirstOrDefault();
            }
        }

        public List<Class> GetByCateClassId(int idParent, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Classes.Where(p => p.CateClassId == idParent && p.isEnable == isEnable).ToList();
            }
        }

        public List<Category> GetByParent(int idParent, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Categories.Where(p => p.Parent == idParent && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
            }
        }

        public List<Category> GetByParent(int idParent, int top)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Categories.Where(p => p.Parent == idParent).Take(top).OrderBy(p => p.Sort).ToList();
            }
        }

        //Get by Level

        public List<Category> GetByLevel(int level)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Categories.Where(p => p.Level == level).OrderBy(p => p.Sort).ToList();
            }
        }

        public List<Category> GetByLevel(int level, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Categories.Where(p => p.Level == level && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
            }
        }

        //Get by Page
        public List<Class> GetByPage(List<Class> list, int page, int size)
        {
            page--;
            int skip = page * size;
            return list.Skip(skip).Take(size).ToList();
        }

        //Get by id
        public Class GetById(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Classes.Where(p => p.Id == id).FirstOrDefault();
            }
        }

        public Class GetById(int id, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Classes.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
            }
        }

        

        public bool IsHasChild(int id)
        {
            if (GetByParent(id).Count == 0)
                return false;
            else return true;
        }

        //Enable

        public void Enable(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Class obj = data.Classes.Where(p => p.Id == id).FirstOrDefault();
                obj.isEnable = !obj.isEnable;

                data.SubmitChanges();
            }
        }
        public void Enable(int id, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Class obj = data.Classes.Where(p => p.Id == id).FirstOrDefault();
                obj.isEnable = isEnable;

                data.SubmitChanges();
            }
        }

        //Add

        public void Add(List<Class> obj)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                data.Classes.InsertAllOnSubmit(obj);
                data.SubmitChanges();
            }
        }

        //Update

        public void Update(Class obj)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Class classes = data.Classes.Where(p => p.Id == obj.Id).FirstOrDefault();

                classes.CreateDate = obj.CreateDate;
                classes.isEnable = obj.isEnable;
                classes.Name = obj.Name;
                data.SubmitChanges();
            }
        }

        //Delete

        public void Delete(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Class obj = data.Classes.Where(p => p.Id == id).FirstOrDefault();
                if (obj != null)
                {
                    data.Classes.DeleteOnSubmit(obj);
                    data.SubmitChanges();
                }
            }
        }
    }


}
